package weaver.jobs.cese;

import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.cese.duss.ceseutil.WorkflowCreateHandler;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-10-30
 * @Description: 定时生成供应商认证待办流程
 * @ModifyLog:
 **/

public class WeaSupplierCertificatCronJob extends BaseCronJob {
    @Override
    public void execute() {
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog( "-----------WeaSupplierCertificatCronJob------------" );
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        RequestInfo requestInfo = new RequestInfo();
        int userid =  requestInfo.getRequestManager().getUserId();
        String creatorId = "";//创建人
        String workflowId = "";//流程ID
        String requestName = "";//流程名称
        String IsNextFlow = "0";//是否提交下一节点
        String WULIAO = "0";//证书类型-物料
        String GONGCHENG = "1";//证书类型-工程
        String FUWU = "2";//证书类型-服务
        String workflowID_wl = baseBean.getPropValue("fk_srm_srm","workflowID_wl");//物料流程ID
        String wl_gws = baseBean.getPropValue("fk_srm_srm","wl_gws");//岗位
        String wl_zsmcs = baseBean.getPropValue("fk_srm_srm","wl_zsmcs");//证书名称
        String wl_sqxmmc = baseBean.getPropValue("fk_srm_srm","wl_sqxmmc");//授权项目名称
        String workflowID_fw = baseBean.getPropValue("fk_srm_srm","workflowID_fw");//服务流程ID
        String fw_zsmcs = baseBean.getPropValue("fk_srm_srm","fw_zsmcs");//授权项目名称
        String workflowID_gc = baseBean.getPropValue("fk_srm_srm","workflowID_gc");//工程流程ID
        String gc_gw1 = baseBean.getPropValue("fk_srm_srm","gc_gw1");//工程-岗位
        String gc_gw9 = baseBean.getPropValue("fk_srm_srm","gc_gw9");//工程-岗位
        String gc_gw10 = baseBean.getPropValue("fk_srm_srm","gc_gw10");//工程-岗位
        String gc_gw11 = baseBean.getPropValue("fk_srm_srm","gc_gw11");//工程-岗位
        String gc_gw12 = baseBean.getPropValue("fk_srm_srm","gc_gw12");//工程-岗位
        String gc_gz12 = baseBean.getPropValue("fk_srm_srm","gc_gz12");//工程-组织
        String gc_zslx4 = baseBean.getPropValue("fk_srm_srm","gc_zslx4");//工程-证书名称
        String gc_zsmc3 = baseBean.getPropValue("fk_srm_srm","gc_zsmc3");//工程-证书名称
        String gc_zsmc4 = baseBean.getPropValue("fk_srm_srm","gc_zsmc4");//工程-证书名称
        String gc_zsmc5 = baseBean.getPropValue("fk_srm_srm","gc_zsmc5");//工程-证书名称
        Map<String, String> requestMainMap = new HashMap<>();
        WorkflowCreateHandler handler = new WorkflowCreateHandler();
        String sql = "select gyslx,gyszh from uf_SRM_gyskpx where qyrzqk='0' and gyslx in(0,1,2)";
        rs.executeQuery( sql );
        while (rs.next()) {
            String gyslx = Util.null2String( rs.getString( "gyslx" ) );
            if (WULIAO.equals( gyslx )) {  //物料供应商认证表单
               Map<String,String> resultMap = loginid2ID( Util.null2String( rs.getString( "gyszh" ) ) );
                creatorId = resultMap.get("id");
                workflowId = workflowID_wl;
                requestName = "供应商管理-物料供应商认证表单-" + DateUtil.getDateTime()+"-"+resultMap.get("lastname");
                requestMainMap.put( "dqdlz",userid+"");
                String requestid = handler.WorkflowCreateByRequestMap( creatorId, workflowId, requestName, IsNextFlow, requestMainMap );
                if(Util.getIntValue(requestid)>0){
                   String fomrid = workflowId2formId(workflowID_wl);
                    rs1.executeQuery("select id from formtable_main_"+fomrid+" where requestid=?",requestid);
                    rs1.next();
                    String mainid = Util.null2String(rs1.getString( "id" ));
                    //明细表1
                    String [] list_gw = wl_gws.split(",");
                    for(String gw:list_gw){
                        rs1.executeUpdate("insert into formtable_main_"+fomrid+"_dt4(mainid,gw)values('"+mainid+"','"+gw+"') where mainid=?",mainid);
                    }
                    //明细表4
                    String [] list_zsmc = wl_zsmcs.split(",");
                    for(String zsmc:list_zsmc){
                        if("52".equals(zsmc)||"53".equals(zsmc)){
                            rs1.executeUpdate("insert into formtable_main_"+fomrid+"_dt4(mainid,zsmc)values('"+mainid+"','"+zsmc+"') where mainid=?",mainid);
                        }
                        rs1.executeUpdate("insert into formtable_main_"+fomrid+"_dt4(mainid,zsmc,sqxmmc)values('"+mainid+"','"+zsmc+"','"+wl_sqxmmc+"') where mainid=?",mainid);
                    }
                }
            } else if (FUWU.equals( gyslx )) { //服务供应商认证表单
                Map<String,String> resultMap = loginid2ID( Util.null2String( rs.getString( "gyszh" ) ) );
                creatorId = resultMap.get("id");
                workflowId = workflowID_fw;
                requestMainMap.put( "dqdlz",userid+"");
                requestName = "供应商管理-服务供应商认证表单-" + DateUtil.getDateTime()+"-"+resultMap.get("lastname");
                String requestid = handler.WorkflowCreateByRequestMap( creatorId, workflowId, requestName, IsNextFlow, requestMainMap );
                if(Util.getIntValue(requestid)>0){
                    String fomrid = workflowId2formId(workflowID_fw);
                    rs1.executeQuery("select id from formtable_main_"+fomrid+" where requestid=?",requestid);
                    rs1.next();
                    String mainid = Util.null2String(rs1.getString( "id" ));
                    //明细表4
                    String [] list_zsmc = fw_zsmcs.split(",");
                    for(String zsmc:list_zsmc){
                            rs1.executeUpdate("insert into formtable_main_"+fomrid+"_dt4(mainid,zsmc)values('"+mainid+"','"+zsmc+"') where mainid=?",mainid);
                    }
                }
            } else if (GONGCHENG.equals( gyslx )) { //工程供应商认证表单
                Map<String,String> resultMap = loginid2ID( Util.null2String( rs.getString( "gyszh" ) ) );
                creatorId = resultMap.get("id");
                workflowId = workflowID_gc;
                requestMainMap.put( "dqdlz",userid+"");
                requestName = "供应商管理-工程供应商认证表单-" + DateUtil.getDateTime()+"-"+resultMap.get("lastname");
                String requestid =  handler.WorkflowCreateByRequestMap( creatorId, workflowId, requestName, IsNextFlow, requestMainMap );
                if(Util.getIntValue(requestid)>0){
                    String fomrid = workflowId2formId(workflowID_gc);
                    rs1.executeQuery("select id from formtable_main_"+fomrid+" where requestid=?",requestid);
                    rs1.next();
                    String mainid = Util.null2String(rs1.getString( "id" ));
                    //明细表1
                    String [] list_gw1 = gc_gw1.split(",");
                    for(String gw1:list_gw1){
                        rs1.executeUpdate("insert into formtable_main_"+fomrid+"_dt1(mainid,gw)values('"+mainid+"','"+gw1+"') where mainid=?",mainid);
                    }
                    //明细表4
                    String [] zslx = gc_zslx4.split(",");
                    for(int i = 0;i<zslx.length;i++){
                        if(i==3){
                            rs1.executeUpdate("insert into formtable_main_"+fomrid+"_dt4(mainid,zslx,zsmc)values('"+mainid+"','"+zslx+"','"+gc_zsmc3+"') where mainid=?",mainid);
                        }else if(i==4){
                            rs1.executeUpdate("insert into formtable_main_"+fomrid+"_dt4(mainid,zslx,zsmc)values('"+mainid+"','"+zslx+"','"+gc_zsmc4+"') where mainid=?",mainid);
                        }else if(i==5){
                            rs1.executeUpdate("insert into formtable_main_"+fomrid+"_dt4(mainid,zslx,zsmc)values('"+mainid+"','"+zslx+"','"+gc_zsmc5+"') where mainid=?",mainid);
                        }else {
                            rs1.executeUpdate("insert into formtable_main_"+fomrid+"_dt4(mainid,zslx)values('"+mainid+"','"+zslx+"') where mainid=?",mainid);
                        }
                    }
                    rs1.executeUpdate("insert into formtable_main_"+fomrid+"_dt9(mainid,gw)values('"+mainid+"','"+gc_gw9+"') where mainid=?",mainid);
                    rs1.executeUpdate("insert into formtable_main_"+fomrid+"_dt10(mainid,gw)values('"+mainid+"','"+gc_gw10+"') where mainid=?",mainid);
                    rs1.executeUpdate("insert into formtable_main_"+fomrid+"_dt11(mainid,gw)values('"+mainid+"','"+gc_gw11+"') where mainid=?",mainid);
                    rs1.executeUpdate("insert into formtable_main_"+fomrid+"_dt12(mainid,gw,gc)values('"+mainid+"','"+gc_gw12+"','"+gc_gz12+"') where mainid=?",mainid);
                }
            }

        }
    }
    //人力资源loginid转id
    public static Map<String, String> loginid2ID(String loginid) {
        Map<String, String> returnMap = new HashMap<>();
        RecordSet rs = new RecordSet();
        String id = "";
        String lastname = "";
        rs.executeQuery( "select id,lastname from hrmresource where loginid='" + loginid + "'" );
        if (rs.next()) {
            id = Util.null2String( rs.getString( "id" ) );
            lastname = Util.null2String( rs.getString("lastname"));
            returnMap.put("id",id);
            returnMap.put("lastname",lastname);
        }
        return returnMap;
    }
    //人力资源loginid转id
    public static String workflowId2formId(String workflowId) {
        RecordSet rs = new RecordSet();
        String formid = "";
        rs.executeQuery( " select abs(formid) from workflow_base where id='"+workflowId+"'" );
        if (rs.next()) {
            formid = Util.null2String( rs.getString(1));
        }
        return formid;
    }
}
